<template>
    <div class="app-container x-container" v-loading="loading">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
            <el-form-item label="排行榜标题" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入排行榜标题" maxlength="50" clearable />
            </el-form-item>
            <el-form-item label="SEO标题" prop="page_title">
                <el-input v-model="ruleForm.page_title" placeholder="请输入SEO标题" maxlength="500" clearable />
            </el-form-item>
            <el-form-item label="SEO关键词" prop="page_keywords">
                <el-input v-model="ruleForm.page_keywords" placeholder="请输入关键词描述" maxlength="500" clearable />
            </el-form-item>
            <el-form-item label="SEO描述" prop="page_description">
                <el-input type="textarea" v-model="ruleForm.page_description" placeholder="请输入SEO描述" maxlength="500"
                    autosize clearable />
            </el-form-item>
            <el-form-item label="下载详情页图(375*120)" prop="image">
                <FileUpload v-model="ruleForm.image" :limit="1"></FileUpload>
            </el-form-item>
            <el-form-item label="关联游戏" prop="game_ids">
                <association type="3" v-model="ruleForm.game_ids"></association>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="ruleForm.status">
                    <el-radio :value="1">是</el-radio>
                    <el-radio :value="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-row>
                <el-col class="flex-center-align" :span="24">
                    <el-button @click="cancelForm(ruleFormRef)">取消</el-button>
                    <!-- <el-button @click="resetForm(ruleFormRef)">重置</el-button> -->
                    <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                    <el-button type="primary" @click="submitForm(ruleFormRef, 1)">提交并发布</el-button>

                </el-col>
            </el-row>
        </el-form>
    </div>

</template>

<script setup>
import { addRanking, editRanking, getRankingDetail } from '@/api/ranking'
import { reactive, ref, inject } from 'vue'
import association from '@/components/association/index.vue'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter();
const route = useRoute();
const used = inject('used');


const ruleFormRef = ref()
const ruleForm = reactive(
    {
        id: null,
        name: '',
        image: '',
        status: 1,
        page_keywords: '',//SEO关键词
        page_title: '',//SEO标题
        page_description: '',//SEO描述
        game_ids: '',//关联游戏
    }
)

const rules = reactive(
    {
        name: [
            { required: true, message: '请输入排行榜标题', trigger: 'blur' },
        ],
        page_keywords: [
            { required: true, message: '请输入关键词描述', trigger: 'blur' },
        ],
        page_title: [
            { required: true, message: '请输入SEO标题', trigger: 'blur' },
        ],
        page_description: [
            { required: true, message: '请输入SEO描述', trigger: 'blur' },
        ],

    }
)


/* 获取详情数据 */
let loading = ref(false);
let id = route.query.id;

const getDetail = async () => {
    const { data } = await getRankingDetail({ id: id })
    data.game_ids = data.game_ids.join(',')
    Object.assign(ruleForm, data)
    // Object.keys(data.row).forEach((key) => {
    //     if (key in ruleForm) {
    //         ruleForm[key] = data.row[key];
    //     }
    // });
    loading.value = false;
}
if (id) {
    loading.value = true;
    getDetail()
}

//提交
const submitForm = async (formEl, type) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            let params = { ...ruleForm };
            type == 1 ? params.status = 1 : '';
            let res = id ? await editRanking(params) : await addRanking(params);
            if (res.code == 1) {
                used.msgSuccess(res.message)
                router.go(-1)
            } else {
                used.msgError(res.message)
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
//取消
const cancelForm = (formEl) => {
    router.go(-1)
}
//重置
const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped lang="scss">
.x-container {
    position: relative;
    padding: 20px;
    background-color: #fff;
}
</style>